﻿using BCL.BaseLib.Basics;
using Devices;
using System;
using System.IO;
using System.Xml.Serialization;
using System.Collections.Generic;
using System.Linq;
using DevicesView.Entity;
using BCL.Tool.Extend;
using DevicesView.Logic.Log;

namespace DevicesView.Logic.App
{
    [Serializable]
    public class AppResource : IResource
    {
        /// <summary>
        /// 配置文件地址
        /// </summary>
        private static string ConfigPath { get; set; }
        /// <summary>
        /// 服务器配置地址
        /// </summary>
        private static string NetConfigPath { get; set; }
        /// <summary>
        /// 医院配置文件地址
        /// </summary>
        private static string HospitalConfig { get; set; }

        /// <summary>
        /// 当前医院的所有医生
        /// </summary>
        public List<Doctor> Doctors { get; set; }





        public static AppResource CurrentResource
        {
            get
            {
                return (AppResource)BCLApplication.Resource;
            }
        }
        public AppResource()
        {
            
        }

        public static AppResource LoadResource()
        {
            AppResource app = null;
            if (File.Exists(ConfigPath))
                app = Tool.GetObjectByXML<AppResource>(ConfigPath);
            else
                app = new AppResource();

            if (File.Exists(NetConfigPath))
                app.NetConfig = Tool.GetObjectByXML<AppNetConfig>(NetConfigPath);
            else
                Tool.ObjectSaveToXML(app.NetConfig = AppNetConfig.CreateNetConfig(), NetConfigPath);

            if (File.Exists(HospitalConfig))
            {
                app.Hospital = Tool.GetObjectByXML<Entity.HospitalInfo>(HospitalConfig);
                if (File.Exists(app.Hospital.LogoPath))
                    app.Hospital.Logo = File.ReadAllBytes(app.Hospital.LogoPath);
                if (File.Exists(app.Hospital.QRCodePath))
                    app.Hospital.QRCode = File.ReadAllBytes(app.Hospital.QRCodePath);
            }
            else
            {
                //HospitalInfo info = DevicesView.Logic.Test.ResultLogic.GetHospital(Computer.CpuID);
                //app.Hospital = new HospitalInfo { Code = info.Code, Name = "宠物医院", Phone = "15801531753", Address = "北京市 昌平区", Logo = File.ReadAllBytes("image\\hc.png"), QRCode = File.ReadAllBytes("image\\xc.png") };
                app.Hospital = new Entity.HospitalInfo { Address="北京市昌平区", Code = "0", Name = "珍爱宠物医院", Phone = "15801531753", Remarks = "请修改", Logo = File.ReadAllBytes("image\\hc.png"), QRCode = File.ReadAllBytes("image\\xc.png") };
            }




            //NetConfig.Servers.ForEach(s =>
            //{
            //    switch (s.ServerName)
            //    {
            //        case "LogicServiceAddress":
            //            LogicServerAddress = s.Address;
            //            break;
            //        case "OSSServer":
            //            OssServerAddress = s.Address;
            //            break;
            //    }
            //});

            return app;
        }

        [Newtonsoft.Json.JsonIgnore]
        [System.Xml.Serialization.XmlIgnore]
        public HospitalInfo Hospital
        {
            get; set;
        }

        [Newtonsoft.Json.JsonIgnore]
        [XmlIgnore]
        public string AppCode { get { return Setup.Logic.AppCode; } }

        public SystemUserInfo LoginUser { get; set; }
        
        static AppResource()
        {
            ConfigPath = AppDomain.CurrentDomain.BaseDirectory + "Config\\Resource.xml";
            NetConfigPath = AppDomain.CurrentDomain.BaseDirectory + "Config\\Net.xml";
            HospitalConfig= AppDomain.CurrentDomain.BaseDirectory + "Config\\Hospital.xml";
            Results = new Dictionary<string, List<Result>>();
            if (!Directory.Exists(Path.GetDirectoryName(ConfigPath)))
                Directory.CreateDirectory(Path.GetDirectoryName(ConfigPath));
        }


        public void Save()
        {

            if (Hospital.Logo == null)
                Hospital.LogoPath = null;
            else
            {
                string HospitalPath = AppDomain.CurrentDomain.BaseDirectory + Hospital.Name;
                if (!Directory.Exists(HospitalPath))
                    Directory.CreateDirectory(HospitalPath);

                Hospital.LogoPath = HospitalPath + "\\logo";
                if (File.Exists(Hospital.LogoPath))
                    File.Delete(Hospital.LogoPath);
                Hospital.Logo?.ToImage().Save(Hospital.LogoPath);

                Hospital.QRCodePath = HospitalPath + "\\QRCode";
                if (File.Exists(Hospital.QRCodePath))
                    File.Delete(Hospital.QRCodePath);
                Hospital.QRCode?.ToImage().Save(Hospital.QRCodePath);
            }
            Tool.ObjectSaveToXML(Hospital, HospitalConfig);
            Tool.ObjectSaveToXML(this, ConfigPath);
        }

        [Newtonsoft.Json.JsonIgnore]
        [XmlIgnore]
        public AppNetConfig NetConfig { get; set; }


        public static string LogicServerAddress { get => CurrentResource.NetConfig.Servers.FirstOrDefault(o=>o.ServerName== "LogicServiceAddress")?.Address; }

        public static string OssServerAddress { get => CurrentResource.NetConfig.Servers.FirstOrDefault(o => o.ServerName == "OSSServer")?.Address; }

        [XmlIgnore]
        public static Dictionary<string, List<Devices.Result>> Results;



    }
}
